package com.ck.client.service.impl;

import com.ck.common.model.bo.DeviceKeyMsg;
import com.ck.common.model.bo.DeviceReportBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ck.client.dao.DeviceInfoMapper;
import com.ck.common.model.Device;
import com.ck.client.service.DeviceInfoService;

import java.util.List;

/**
 * 终端Service
 */
@Service(value = "deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService {
	
	@Autowired
	private DeviceInfoMapper deviceInfoMapper;

	/**
	 *  首次联网存入终端信息
	 *
	 * @param device Device类型对象
	 */
	@Override
	@Transactional( rollbackFor = Exception.class )
	public boolean register(Device device) {
		int effect = deviceInfoMapper.registerDevice(device);
		return effect > 0;
	}

	/**
	 * 获取终端 
	 */
	@Override
	public Device getDeviceInfoByCode(String deviceCode) {
		return deviceInfoMapper.getDeviceInfoByCode(deviceCode);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean report(DeviceReportBO reportDevice) {
		int effect = deviceInfoMapper.deviceReport(reportDevice);
		return effect > 0;
	}

	@Override
	@Transactional( rollbackFor = Exception.class)
	public boolean updateDeviceUpgradeInfo(String deviceCode,String softwareVer, Long softwareUpdateTime, String firmwareVer,Long firmwareUpdateTime) {
		int effect = deviceInfoMapper.updateDeviceUpgradeInfo(deviceCode,softwareVer,softwareUpdateTime,firmwareVer,firmwareUpdateTime);
		return effect > 0;
	}

	@Override
	public boolean isAlreadyExists(String deviceCode) {
		int count = deviceInfoMapper.isAlreadyExists(deviceCode);
		return count > 0;
	}

	@Override
	public DeviceKeyMsg getDeviceKeyMsg(String deviceCode) {
		return deviceInfoMapper.getDeviceKeyMsg(deviceCode);
	}

	@Override
	public boolean secondRegister(String deviceCode, String token, Long deviceGroupId) {
		int count = deviceInfoMapper.secondRegister(deviceCode,token,deviceGroupId,System.currentTimeMillis());
		return count > 0;
	}

	@Override
	public void setDeviceInfo(String deviceCode,String deviceMac, String modelName, String resolution) {
		deviceInfoMapper.setDeviceInfo(deviceCode,deviceMac,modelName,resolution);
	}

	@Override
	public List<String> getOffLineDevice(Long timeDiff) {
		return deviceInfoMapper.getOffLineDevice(timeDiff);
	}

	@Override
	public int setDeviceOffLine(Long timeDiff) {
		return deviceInfoMapper.setDeviceOffLine(timeDiff);
	}

	@Override
	public String getDeviceResolution(String deviceCode) {
		return deviceInfoMapper.getDeviceResolution(deviceCode);
	}

}
